package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSProperties;
import com.itheima.utils.AliyunOSSUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
// 阿里云文件上传
public class UploadController {
    @Autowired
    private AliyunOSSProperties  aliyunOSSProperties;
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传开始,文件名{}", file.getOriginalFilename());
        // 获取字节数组
        byte[] bytes = file.getBytes();
        // 获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), bytes, exName);
        return Result.success(url);
    }
}
